package com.bigfreakindeals.Protocols.SoapHandlers;

import java.io.IOException;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;
import com.bigfreakindeals.Models.UniversityDeals;
import com.bigfreakindeals.Models.UserProfile;

public class FAQHandler  {
	
	
		// names of the XML tags
		static final String STATUS = "status";

		static final String ITEM = "item";
		static final String PAGES = "pages";
		/* individual city items */
		static final String SLUG = "slug";
		static final String TITLE = "title";
		static final String CONTENT = "content";
		
		static final String ERROR = "error";
		private final String dealFaq="deal-faq";
	//	private final String faq="deal-faq";
		private final String termsanconditions="term-and-conditions";
		
		String feedUrl;

		private static final String SOAP_ACTION = "1";
		private static final String METHOD_NAME = "bigfreakindeals";
		private static final String NAMESPACE = "1";

		public FAQHandler(String feedUrl) {
			this.feedUrl = feedUrl;
			parse();
		}

		public void parse() {

			
			try {

				SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				envelope.setOutputSoapObject(request);

				HttpTransportSE androidHttpTransport = new HttpTransportSE(feedUrl);

				androidHttpTransport.call(SOAP_ACTION, envelope);
				SoapObject response = (SoapObject) envelope.getResponse();

				String status = response.getProperty(STATUS).toString();
			
				if (status != null && status.length() > 0) {
					if (status.contentEquals("false")) {
						// error on active deals
						// get error message

						// TODO ERROR HANDLING
						Intent intent = new Intent(Constant.EVENTS_GETSCHOOLLIST_ERROR);
						intent.putExtra(Constant.TAG_WEBSERVICE_ERROR_MESSAGE, "");
						ApplicationEx.getInstance().sendBroadcast(intent);
						return;

					} else {// status = true; active deals exist

						Vector items = (Vector) response.getProperty(PAGES);
						for (int count = 0; count <= items.size() - 1; count++) {

							SoapObject deals = (SoapObject) items.get(count);
							String slug="", title="", contents=""; 
							
							if (deals.getProperty(SLUG) != null && (deals.getProperty(SLUG).toString()).length() > 0)
								slug = deals.getProperty(SLUG).toString();

							if (deals.getProperty(TITLE) != null && (deals.getProperty(TITLE).toString()).length() > 0)
								title = deals.getProperty(TITLE).toString();

							if (deals.getProperty(CONTENT) != null && (deals.getProperty(CONTENT).toString()).length() > 0)
								contents = deals.getProperty(CONTENT).toString();

							if (slug.compareTo(dealFaq) == 0&& contents!=null){ 
								//both uses the same content
								ApplicationEx.getInstance().myProfile.setDEALSFAQ(contents);
								ApplicationEx.getInstance().myProfile.setFAQ(contents);
							}else if (slug.compareTo(termsanconditions) == 0 && contents!=null){
								ApplicationEx.getInstance().myProfile.setTAndC(contents);
										
							}

						}// end for

					}

				}

			} catch (IOException e1) {
				// TODO Auto-generated catch block
				Intent intent = new Intent(Constant.EVENTS_HTTPCONNECTION_ERROR);
				intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
				ApplicationEx.getInstance().sendBroadcast(intent);
				// TODO Auto-generated catch block

			} catch (Exception e1) {
				Intent intent = new Intent(Constant.EVENTS_HTTPCONNECTION_ERROR);
				intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
				ApplicationEx.getInstance().sendBroadcast(intent);
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			
				Intent intent = new Intent(Constant.EVENTS_FAQ);
				ApplicationEx.getInstance().sendBroadcast(intent);

			// return closestUniversity;
		}

	}
